package com.fucheng.shortlink.admin.common.biz.user;

import cn.hutool.core.util.StrUtil;
import com.fucheng.shortlink.admin.common.constant.UserConstant;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

/**
 * 用户信息传输过滤器
 */
@RequiredArgsConstructor
@Slf4j
public class UserTransmitFilter implements Filter {

//    @Override
//    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
//            throws IOException, ServletException {
//        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
//        String userId = httpServletRequest.getHeader(UserConstant.USER_ID_KEY);
//
//        // 更严格的验证
//        if (StringUtils.hasText(userId)) {
//            try {
//                String userName = httpServletRequest.getHeader(UserConstant.USER_NAME_KEY);
//                String realName = httpServletRequest.getHeader(UserConstant.REAL_NAME_KEY);
//
//                // 解码处理并增加异常处理
//                if (StringUtils.hasText(userName)) {
//                    userName = URLDecoder.decode(userName, StandardCharsets.UTF_8.toString());
//                }
//                if (StringUtils.hasText(realName)) {
//                    realName = URLDecoder.decode(realName, StandardCharsets.UTF_8.toString());
//                }
//
//                // 构建用户信息前进行验证
//                if (StringUtils.hasText(userName)) {
//                    UserInfoDTO userInfoDTO = UserInfoDTO.builder()
//                            .userId(userId)
//                            .username(userName)
//                            .realName(realName)
//                            .build();
//                    UserContext.setUser(userInfoDTO);
//                }
//            } catch (Exception e) {
//                // 记录日志，但不中断流程
//                log.warn("Failed to process user info from headers", e);
//            }
//        }
//
//        try {
//            filterChain.doFilter(servletRequest, servletResponse);
//        } finally {
//            UserContext.removeUser();
//        }
//    }

    @SneakyThrows
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) {
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        String username = httpServletRequest.getHeader("username");
        if (StrUtil.isNotBlank(username)) {
            String userId = httpServletRequest.getHeader("userId");
            String realName = httpServletRequest.getHeader("realName");
            UserInfoDTO userInfoDTO = new UserInfoDTO(userId, username, realName);
            UserContext.setUser(userInfoDTO);
        }
        try {
            filterChain.doFilter(servletRequest, servletResponse);
        } finally {
            UserContext.removeUser();
        }
    }

}